/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import POJO.POJO_TuyenTau;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author ben
 */
public class DAO_TuyenTau {
    
    DataProvider data;
    
    public List<POJO_TuyenTau> LoadList() throws SQLException {
        data = new DataProvider();
        data.Open();
        
        String query = "SELECT * FROM tuyen_tau";
        List<POJO_TuyenTau> lst = new ArrayList<>();
        ResultSet rs = data.ExecuteQuery(query);
            
        while (rs.next()) {
            POJO_TuyenTau tt = new POJO_TuyenTau();
            tt.setMATUYEN(rs.getString("MaTuyen"));
            tt.setTENTUYEN(rs.getString("TenTuyen"));
            
            lst.add(tt); 
        }
        
        return lst;
    } 
    
    public int themTuyenTau(POJO_TuyenTau tt) {
        data = new DataProvider();
        data.Open();
        String sql = String.format("Insert into tuyen_tau values ('%s','%s')", 
                tt.getMATUYEN(), 
                tt.getTENTUYEN());
            
        return data.ExeCuteUpdate(sql);
    } 
    
    public int xoaTuyenTau(POJO_TuyenTau tt) {
        
            data = new DataProvider();
            data.Open();
            
            String sql = String.format("DELETE FROM tuyen_tau WHERE MaTuyen = '%s'", tt.getMATUYEN());
            
            return data.ExeCuteUpdate(sql);
    }
    
    
    
    public int capnhatTuyenTau(POJO_TuyenTau tt)  {
        data = new DataProvider();
        data.Open();
        
        String sql = String.format("UPDATE tuyen_tau SET TenTuyen = '%s' where MaTuyen = '%s'",  
                    tt.getTENTUYEN(),
                    tt.getMATUYEN());        
            
            
        return data.ExeCuteUpdate(sql);
    }
    
}
